package provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.util.Log;

import com.example.server.UserInfoContent;

import database.UserDBHelper;

public class UserInfoProvider extends ContentProvider {
    private UserDBHelper mHelper;
    public UserInfoProvider() {
    }
    private static final int USER = 1;
    private  static final int USERS = 2;
    private static final UriMatcher URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
    static {
        URI_MATCHER.addURI(UserInfoContent.AUTHORITIES,"/user",USERS);
        URI_MATCHER.addURI(UserInfoContent.AUTHORITIES,"/user/#",USER);
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // Implement this to handle requests to delete one or more rows.
        int count = 0;
        switch (URI_MATCHER.match(uri)) {
            case USER:
                String id = uri.getLastPathSegment();
                SQLiteDatabase db1 = mHelper.getWritableDatabase();
                count = db1.delete(UserDBHelper.TABLE_NAME, "id=?", new String[]{id});
                db1.close();
                break;
            case USERS:
                SQLiteDatabase db= mHelper.getReadableDatabase();
                count = db.delete(UserDBHelper.TABLE_NAME, selection, selectionArgs);
                db.close();
                break;
        }
        return count;
    }

    @Override
    public String getType(Uri uri) {
        // TODO: Implement this to handle requests for the MIME type of the data
        // at the given URI.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    //
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO: Implement this to handle requests to insert a new row.
        Log.e("TAG", "insert: " );
        SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取数据库实例
        db.insert(UserDBHelper.TABLE_NAME,null,values);
        return uri;// 返回通用资源标识符
    }

    @Override
    public boolean onCreate() {
        // TODO: Implement this to initialize your content provider on startup.
        Log.e("TAG", "onCreate:789456666 " );
        mHelper=  UserDBHelper.getInstance(getContext());
        return true;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        // TODO: Implement this to handle query requests from clients.
        Log.e("TAG", "query: ");
        SQLiteDatabase db = mHelper.getReadableDatabase();
       return db.query(UserDBHelper.TABLE_NAME,null,selection,selectionArgs,null,null,sortOrder);
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        // TODO: Implement this to handle requests to update one or more rows.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}